Computing or retrieving a value only to then immediately overwrite it or throw it away indicates a serious logic error in the code.
Assigning a value to a local variable that is not read by any subsequent instruction is called a dead store. The following code snippet
depicts a few dead stores.
int foo() {
int x = 0; // Noncompliant: dead store, next line overwrites x
x = 100; // Noncompliant: dead store, next line overwrites x
x = 200;
int y = 0;
y += 9001; // Noncompliant: dead store, y is never used
int z = 300; // Noncompliant: dead store, next line overwrites z
z = 400;
return x + z * 2;
}
Even if the unnecessary operations do not do any harm in terms of the program’s correctness, they are—at best—a waste of computing resources. In
most cases, these operations have their intended use but it is not expressed correctly in the code. Therefore, unused values and superfluous code
should be removed to prevent logic errors.